import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:keyboard_avoider/keyboard_avoider.dart';

class DialogAction {
  final String title;
  bool isCancel;
  final GestureTapCallback? action;

  DialogAction({
    required this.title,
    this.isCancel = false,
    this.action,
  });

  style() {
    return TextStyle(
        fontSize: 15,
        fontWeight: FontWeight.w700,
        color: Color(isCancel ? 0xff333333 : 0xff3f74ef));
  }

  rowAcitonItem(BuildContext context) {
    return Expanded(
        child: GestureDetector(
            onTap: () {
              if (action != null) {
                action!();
              }
              Navigator.of(context).pop();
            },
            child: SizedBox(
              height: 50,
              child: Center(
                child: Text(
                  title,
                  style: style(),
                ),
              ),
            )));
  }

  columnActionItem(BuildContext context) {
    return GestureDetector(
        onTap: () {
          if (action != null) {
            action!();
          }
          Navigator.of(context).pop();
        },
        child: SizedBox(
            height: 50, child: Center(child: Text(title, style: style()))));
  }

  static rowItems(List<DialogAction> actions, BuildContext context) {
    var arr = <Widget>[];
    for (var i = 0; i < actions.length; i++) {
      arr.add(actions[i].rowAcitonItem(context));
      if (actions.length == 1) continue;
      if (i < actions.length - 1) {
        arr.add(Container(
          height: 50,
          width: 1,
          color: Color(0xffdddddd),
        ));
      }
    }
    return arr;
  }

  static columnItems(List<DialogAction> actions, BuildContext context) {
    var arr = <Widget>[];
    for (var i = 0; i < actions.length; i++) {
      arr.add(actions[i].columnActionItem(context));
      if (actions.length == 1) continue;
      if (i < actions.length - 1) {
        arr.add(Container(
          height: 1,
          color: Color(0xffdddddd),
        ));
      }
    }
    return arr;
  }
}

class AppDialog extends Dialog {
  final String? title;
  final TextStyle? titleStyle;
  final String? content;
  final TextStyle? contentStyle;
  final Widget? middleWidge;
  final List<DialogAction>? actions;
  final BuildContext context;

  AppDialog(
      {required this.context,
      this.title,
      this.titleStyle,
      this.content,
      this.contentStyle,
      this.middleWidge,
      this.actions,
      super.key});

  @override
  Widget build(BuildContext context) {
    bool isTitleEmpty = title == null || title!.isEmpty;
    bool isContentEmpty = content == null || content!.isEmpty;
    return KeyboardAvoider(
      autoScroll: true,
      child: Material(
          type: MaterialType.transparency,
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
            Container(
                decoration: ShapeDecoration(
                    color: Color(0xFFFFFFFF),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(
                      Radius.circular(18.0),
                    ))),
                margin: EdgeInsets.all(18.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      padding: EdgeInsets.only(
                          top: isTitleEmpty ? 0 : 18, left: 15, right: 15),
                      child: isTitleEmpty
                          ? SizedBox(
                              height: 0,
                            )
                          : Text(
                              title!,
                              style: titleStyle ??
                                  TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: 18,
                                      fontWeight: FontWeight.w700),
                            ),
                    ),
                    Container(
                      padding: EdgeInsets.only(
                          top: isContentEmpty ? 0 : 15, left: 15, right: 15),
                      child: isContentEmpty
                          ? SizedBox(
                              height: 0,
                            )
                          : Text(
                              content!,
                              style: titleStyle ??
                                  TextStyle(
                                    color: Color(0xff333333),
                                    fontSize: 14,
                                  ),
                            ),
                    ),
                    middleWidge ??
                        SizedBox(
                          height: 20,
                        ),
                    actions == null
                        ? SizedBox(
                            height: 0,
                          )
                        : Container(
                            width: double.infinity,
                            height: 1,
                            color: Color(0xffdddddd),
                          ),
                    actions!.length == 2
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: DialogAction.rowItems(actions!, context))
                        : Column(
                            children:
                                DialogAction.columnItems(actions!, context))
                  ],
                ))
          ])),
    );
  }
}
